package main

import (
	"bufio"
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
)

const (
	HttpPrefix = "http://"
)

func main() {
	for _, url := range os.Args[1:] {
		if !strings.HasPrefix(url, HttpPrefix) {
			url = HttpPrefix + url
		}
		resp, err := http.Get(url)
		if err != nil {
			fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
			os.Exit(1)
		}
		if resp.StatusCode == http.StatusOK {
			for key, values := range resp.Header {
				fmt.Printf("%s : %v \n", key, values)
			}
			// body, err := ioutil.ReadAll(resp.Body)
			wt := bufio.NewWriter(os.Stdout)
			_, err := io.Copy(wt, resp.Body)
			if err != nil {
				fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
				os.Exit(1)
			}
			wt.Flush()
		}
	}
}
